package com.wuxiaoqin.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaoqin.admin.entity.*;
import com.wuxiaoqin.admin.entity.goodParam.GoodsAdd;
import com.wuxiaoqin.admin.entity.goodParam.GoodsAll;
import com.wuxiaoqin.admin.entity.goodParam.GoodsImgModify;
import com.wuxiaoqin.admin.entity.goodParam.GoodsSales;
import com.wuxiaoqin.admin.mapper.*;
import com.wuxiaoqin.admin.service.ICommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuxiaoqin.uitls.imgUpload.UploadImgUtils;
import com.wuxiaoqin.uitls.respObj.RespBean;
import com.wuxiaoqin.uitls.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-11-02
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements ICommodityService {


    @Autowired(required = false)
    private CommodityMapper commodityMapper;
    @Autowired(required = false)
    private FirstclassMapper firstclassMapper;
    @Autowired(required = false)
    private SecondclassMapper secondclassMapper;
    @Autowired
    private GoodsstockMapper goodsstockMapper;
    @Autowired
    private GoodsimgMapper goodsimgMapper;



    /**
     * 商品分类
     * @return
     */
    @Override
    public RespBean selectClass() {
        List<Firstclass> firstAllClass = firstclassMapper.getAllClass();
        List<Secondclass> secondAllClass = secondclassMapper.getAllClass();
        Map<String, Object> map = new HashMap<>();
        map.put("firstClass", firstAllClass);
        map.put("secondClass", secondAllClass);
        return RespBean.success("查询成功",map);
    }

    /**
     * 添加商品
     * @param file
     * @param goodsAdd
     * @return
     * @throws IOException
     */
    @Override
    public RespBean goodsAdd(MultipartFile file, GoodsAdd goodsAdd) throws IOException {

        UploadImgUtils uploadImgUtils = new UploadImgUtils();
        String path = uploadImgUtils.getPath();
        System.out.println(path);
        String updateName = uploadImgUtils.imageName(file.getOriginalFilename());
        System.out.println(updateName);
        boolean b = uploadImgUtils.upLoadImage(path, updateName, file.getInputStream());
        if (!b){
            return RespBean.error("图片上传失败");
        }

        Commodity c_id = commodityMapper.selectOne(new QueryWrapper<Commodity>().orderByDesc("C_id").last("limit 1"));
        Integer newId = c_id.getCId()+1;
        Commodity commodity = new Commodity();
        commodity.setCId(newId);
        commodity.setCName(goodsAdd.getCName());
        commodity.setCPrice(goodsAdd.getCPrice());
        commodity.setCSecondclass(goodsAdd.getCSecondclass());
        commodity.setCDescribe(goodsAdd.getCDescribe());
        commodity.setCFirstclass(goodsAdd.getCFirstclass());
        commodity.setCState(goodsAdd.getCState());
        commodity.setCSalesvolumes(0);
        commodity.setCImg("img\\"+updateName);

        int insert = commodityMapper.insert(commodity);
        if (insert<=0){
            return RespBean.error("商品添加失败");
        }
        Goodsstock goodsstock = new Goodsstock();
        goodsstock.setCId(newId);
        goodsstock.setAverage(goodsAdd.getAverageStock());
        goodsstock.setS(goodsAdd.getSStock());
        goodsstock.setXs(goodsAdd.getXsStock());
        goodsstock.setM(goodsAdd.getMStock());
        goodsstock.setL(goodsAdd.getLStock());
        goodsstock.setXl(goodsAdd.getXlStock());
        goodsstock.setXxl(goodsAdd.getXxlStock());
        int insert1 = goodsstockMapper.insert(goodsstock);
        if (insert1<0){
            return RespBean.error("库存更新失败");
        }
        return RespBean.success("新增成功",new HashMap<String,Integer>().put("goodsId", newId));
    }

    /**
     *查询所有商品
     * @return
     * @param pn
     * @param pnSize
     */
    @Override
    public Page<GoodsAll> getGoods(Integer pn, Integer pnSize) {
        List<GoodsAll> goodsList = new ArrayList<>();
        List<Commodity> commodities = commodityMapper.selectList(new QueryWrapper<Commodity>());
            for (Commodity commodity : commodities) {
               GoodsAll goodsAll;
                Goodsstock goodsstock = goodsstockMapper.selectById(commodity.getCId());
                if (goodsstock!=null){
                    goodsAll = new GoodsAll(commodity, goodsstock);
                }else {
                    goodsAll = new GoodsAll(commodity, null);
                }
                goodsList.add(goodsAll);
            }
        Page<GoodsAll> result = new Page<>(pn,pnSize,goodsList.size());
        Long pages = result.getPages();

        Integer size =goodsList.size() % pnSize;
        List<GoodsAll> goodsAllList =null;
        if (pages.intValue() == pn){

            goodsAllList = goodsList.subList((pn - 1) * pnSize, (pn - 1) * pnSize + size);
        }else {

           goodsAllList = goodsList.subList((pn - 1) * pnSize, (pn - 1) * pnSize + pnSize);
        }

        result.setRecords(goodsAllList);

        return result;
    }

    @Override
    public Commodity getGoodsById(Integer id) {
        Commodity commodity = commodityMapper.selectById(id);
        return commodity;
    }

    @Override
    public RespBean deleteGoodById(Integer id) {
        int i = commodityMapper.deleteById(id);
        if (i<=0){
            return RespBean.error("删除失败");
        }
        return RespBean.success("删除成功，记得刷新列表");
    }

    /**
     * 单个商品id
     * @param id
     * @return
     */
    @Override
    public Goodsstock getGoodsStockById(Integer id) {
        Goodsstock goodsstock = goodsstockMapper.selectById(id);
        return goodsstock;
    }

    /**
     * 查询附加图片
     * @param id
     * @return
     */
    @Override
    public Goodsimg getGoodsImg(Integer id) {
        Goodsimg goodsimg = goodsimgMapper.selectById(id);
        return goodsimg;
    }

    /**
     * 修改上传图片
     * @param id
     * @param goodsImgModify
     * @return
     * @throws IOException
     */
    @Override
    public RespBean modifyGoodsImg(Integer id, GoodsImgModify goodsImgModify) throws IOException {


        UploadImgUtils uploadImgUtils = new UploadImgUtils();

        String path1 = uploadImgUtils.getPath();
        System.out.println(path1);
        String updateName1 = uploadImgUtils.imageName(goodsImgModify.getFileImg1().getOriginalFilename());
        System.out.println(updateName1);
        uploadImgUtils.upLoadImage(path1, updateName1, goodsImgModify.getFileImg1().getInputStream());

        String path2 = uploadImgUtils.getPath();
        System.out.println(path2);
        String updateName2 = uploadImgUtils.imageName(goodsImgModify.getFileImg2().getOriginalFilename());
        System.out.println(updateName2);
        uploadImgUtils.upLoadImage(path2, updateName2, goodsImgModify.getFileImg2().getInputStream());

        String path3 = uploadImgUtils.getPath();
        System.out.println(path3);
        String updateName3 = uploadImgUtils.imageName(goodsImgModify.getFileImg3().getOriginalFilename());
        System.out.println(updateName3);
        uploadImgUtils.upLoadImage(path3, updateName3, goodsImgModify.getFileImg3().getInputStream());

        String path4 = uploadImgUtils.getPath();
        System.out.println(path4);
        String updateName4 = uploadImgUtils.imageName(goodsImgModify.getFileImg4().getOriginalFilename());
        System.out.println(updateName4);
        uploadImgUtils.upLoadImage(path4, updateName4, goodsImgModify.getFileImg4().getInputStream());

        Goodsimg goodsimg = new Goodsimg();
        goodsimg.setCId(id);
        goodsimg.setImg1("img\\"+updateName1);
        goodsimg.setImg2("img\\"+updateName2);
        goodsimg.setImg3("img\\"+updateName3);
        goodsimg.setImg4("img\\"+updateName4);
        Goodsimg goodsimg1 = goodsimgMapper.selectById(id);
        if (goodsimg1 != null){
            int i = goodsimgMapper.updateById(goodsimg);
            if (i==0){
                return RespBean.error("更改失败");
            }
        }else {
            int i = goodsimgMapper.insert(goodsimg);
            if (i==0){
                return RespBean.error("插入失败");
            }
        }

        return RespBean.success("修改成功！");
    }


    /**
     *
     * @param id
     * @return
     */
    @Override
    public RespBean delGoodsImg(Integer id) {
        int i = goodsimgMapper.deleteById(id);
        if (i==0){
            return RespBean.error("删除失败");
        }

        return RespBean.success("删除成功");
    }

    @Autowired
    private CommoditySalesMapper commoditySalesMapper;

    @Override
    public Page<GoodsSales> salesVolume(Integer pn, Integer size) {
        Integer count = commodityMapper.selectCount(new QueryWrapper<Commodity>());
        int begin = (pn- 1) * size;
        List<GoodsSales> goodsSales = commoditySalesMapper.selectForPage(begin, size);

        Page<GoodsSales> goodsSalesPage = new Page<>(pn, size,count);
        goodsSalesPage.setRecords(goodsSales);

        return goodsSalesPage;
    }


}
